package main.java.org.controller;
import lombok.extern.slf4j.Slf4j;
import main.java.Exam.pojo.Entity.User;
import main.java.Exam.pojo.tableDTO.UserDTO;
import main.java.org.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private main.java.org.service.UserService userService;
//登入接口
    @PostMapping("/login")
    public Result<UserDTO> login(@RequestBody UserDTO userDTO) {
        log.info("正在登入");
        //UserVO userVO = userService.login(userDTO);
        log.info("角色ID:{}",userDTO.getRoleId());
        return Result.success(userService.login(userDTO));
    }
//注册接口
@PostMapping("/register")
public Result<String> register(@RequestBody  UserDTO userDTO) { // @Valid 触发DTO参数校验
    log.info("用户注册：{}", userDTO.getUserAccount());
    userService.register(userDTO);
    return Result.success("注册成功");
}
//发送验证码
    @PostMapping("/alter")
    public Result<String>send(@RequestBody UserDTO userDTO){
        log.info("发送验证码");
        userService.alter(userDTO);
        return Result.success("发送验证码成功");
    }
//修改密码
    @PostMapping("/password")
    public Result<String> resetPassword( @RequestBody UserDTO dto) {
        if(dto.getPassword()==null){
            throw new IllegalArgumentException("请输入修改后的密码");
        }
        userService.resetPassword(dto);
        return Result.success("密码重置成功，请使用新密码登录");
    }
// 个人中心
   @GetMapping("/getuser")
  public Result<User> getuser(@RequestParam Long id){
        userService.getuser(id);
        log.info("个人信息返回数据成功");
        return Result.success(userService.getuser(id));
   }
//更新用户
    @PutMapping("/update")
    public Result getuser1(@RequestBody User user){
        userService.updatauser(user);
        log.info("个人信息更新成功");
        return Result.success("个人信息更新成功");
    }

}
